package com.khyr.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.Dao.*;
import com.khyr.Service.OrderForGoodsService;
import com.khyr.entity.*;
import com.khyr.feign.UserFeign;
import com.khyr.vo.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author ：lyk
 * @date ：Created in 2025/3/17 20:24
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class OrderForGoodsServiceImpl implements OrderForGoodsService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private CareOrdersDao careOrderDao;
    @Autowired
    private HisRegistrationDao hisRegistrationDao;
    @Autowired
    private HisCareHistoryDao hisCareHistoryDao;
    @Autowired
    private CareHistoryDao careHistoryDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private DeptDao deptDao;
    @Autowired
    private CareOrderItemDao careOrderItemDao;
    @Override
    public QMyPageDto getRegistrationByStatus(GetRegistrationByStatusOrderQueryVO getRegistrationByStatusOrderQueryVO) {
        String orderStatus = String.valueOf(getRegistrationByStatusOrderQueryVO.getOrderState());
        String token = request.getHeader("token");
        HisPatient patient = userFeign.getPatientInfo(token).getData();
        QueryWrapper<HisRegistration> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("patient_id",patient.getPatientId());
        queryWrapper.eq("registration_status","3");
        Page<HisRegistration> page = new Page<>(getRegistrationByStatusOrderQueryVO.getPage(), getRegistrationByStatusOrderQueryVO.getPageSize());
        Page<HisRegistration> p = hisRegistrationDao.selectPage(page, queryWrapper);
        ArrayList<OrderInquiryVo> orderInquiryVos = new ArrayList<>();
        QMyPageDto qMyPageDto = new QMyPageDto(
                p.getTotal(),
                p.getCurrent(),
                p.getSize(),
                orderInquiryVos
        );
        for (HisRegistration registration : p.getRecords()) {
            int itemCount=0;
            BigDecimal totalMoney = BigDecimal.ZERO;
            Dept dept = deptDao.selectById(registration.getDeptId());
            User user = userDao.selectById(String.valueOf(registration.getUserId()));
            QueryWrapper<HisCareHistory> hisCareHistoryQueryWrapper =new QueryWrapper<>();
            hisCareHistoryQueryWrapper.eq("reg_id",registration.getRegistrationId());
            HisCareHistory hisCareHistory = hisCareHistoryDao.selectOne(hisCareHistoryQueryWrapper);
            QueryWrapper<CareOrder> careOrderQueryWrapper =new QueryWrapper<>();
            careOrderQueryWrapper.eq("ch_id",hisCareHistory.getChId());
            List<CareOrder> careOrders = careOrderDao.selectList(careOrderQueryWrapper);
            OrderInquiryVo orderInquiryVo = new OrderInquiryVo(registration, user, dept, hisCareHistory,null,null, null);
            ArrayList<CareOrder> cOS = new ArrayList<>();
            for (CareOrder careOrder : careOrders){
                QueryWrapper<CareOrderItem> careOrderItemQueryWrapper =new QueryWrapper<>();
                careOrderItemQueryWrapper.eq("co_id",careOrder.getCoId());
                if (!Objects.equals(orderStatus, "4")){
                    careOrderItemQueryWrapper.eq("status",orderStatus);
                }
                List<CareOrderItem> careOrderItems = careOrderItemDao.selectList(careOrderItemQueryWrapper);
                if (!careOrderItems.isEmpty()){
                    for(CareOrderItem item : careOrderItems){
                        itemCount++;
                        totalMoney = totalMoney.add(item.getAmount());
                    }
                    cOS.add(careOrder);
                }
            }
            if (!cOS.isEmpty()) {
                orderInquiryVo.setCareOrders(cOS);
                orderInquiryVo.setTotalNum(itemCount);
                orderInquiryVo.setTotalMoney(totalMoney);
                orderInquiryVos.add(orderInquiryVo);
            }
        }
        qMyPageDto.setItems(orderInquiryVos);
        return qMyPageDto;
    }
    @Override
    public OrderDetailsVo getMemberOrderDetails(String chId, Integer orderState) {
        CareHistory careHistory = careHistoryDao.selectById(chId);
        List<CareOrder> careOrders = careOrderDao.getCareOrderItemByChId(chId,orderState);
        BigDecimal totalMoney = BigDecimal.ZERO;
        ArrayList<CareOrderDto> careOrderDtos = new ArrayList<>();

        for (CareOrder careOrder : careOrders){
            careOrderDtos.add(new CareOrderDto(careOrder, careOrder.getCareOrderItems()));
            List<CareOrderItem> careOrderItems = careOrder.getCareOrderItems();
            for (CareOrderItem careOrderItem : careOrderItems){
                totalMoney = totalMoney.add(careOrderItem.getAmount());
            }
        }
        return new OrderDetailsVo(careHistory,careOrderDtos,totalMoney,orderState);
    }
}
